/**
 * StringDateConverter - Converts To/From ISO Date Strings
 *
 * @author Chris Pratt
 *
 * 2/27/2012
 */
package com.anodyzed.onyx.type;

import com.anodyzed.onyx.util.Convert;

import java.util.Date;

public class StringDateConverter implements Converter {

  /**
   * Convert Strings (or CharSequence's) to/from Date's
   *
   * @param from Either a CharSequence or an Date 
   * @param to The type to convert to 
   * @return Either an Date or a String
   */
  @Override
  public <T> T convert (Object from,Class<T> to) {
    if(from instanceof CharSequence) {
      return to.cast(Convert.toDate(from.toString()));
    } else if(from instanceof Date) {
      return to.cast(Convert.toISOString((Date)from));
    }
    throw new ConversionException("{0.class.name} is neither a String (CharSequence) nor a Date",from);
  } //convert

} //*StringDateConverter
